package com.example.androidbookmarks;

import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.provider.Browser;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv;
	private Button history, appRunning, starServer;
	private boolean flagService = false;
	private Calendar cal = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv = (TextView) findViewById(R.id.tv);
		history = (Button) findViewById(R.id.history);
		appRunning = (Button) findViewById(R.id.apprunning);
		starServer = (Button) findViewById(R.id.starserver);
		
		history.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				getHistory();
			}
		});
		
		appRunning.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				getAppRunning();
			}
		});
		
		starServer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(!flagService){
					Intent intent = new Intent(MainActivity.this, MyService.class);
					stopService(intent);
					intent = new Intent(MainActivity.this, MyService.class);
					startService(intent);
					starServer.setText("Stop Send");
					flagService = true;
				} else {
					Intent intent = new Intent(MainActivity.this, MyService.class);
					stopService(intent);
					starServer.setText("Send Data");
					flagService = false;
				}
			}
		});
	}
	
	public void getHistory()  {		
        //@SuppressWarnings("deprecation")
		String result = "";
		int i = 1;
		Cursor mCur = managedQuery(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, null);
        mCur.moveToFirst();
        if (mCur.moveToFirst() && mCur.getCount() > 0) {
            while (mCur.isAfterLast() == false) {
            	long dateTime = Long.parseLong(mCur.getString(Browser.HISTORY_PROJECTION_DATE_INDEX));
                cal.setTimeInMillis(dateTime);
            	result += i + ") "+ mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX) + " - "+ cal.getTime().toString() + "\n";
                mCur.moveToNext();
                i++;
           }
        }
        tv.setText(result);
	}
	
	public void getAppRunning()  {
		String result = "";
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> listApp = activityManager.getRunningAppProcesses();
		for (int i = 0; i < listApp.size(); i++){
			result += "app " + i + ": " + listApp.get(i).processName.toString() + "\n";
		}
		tv.setText(result);
	}

}
